package com.lovestudying.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lovestudying.dao.AccountDao;
import com.lovestudying.dao.CourseDao;
import com.lovestudying.dao.OrderDao;
import com.lovestudying.dao.Impl.AccountDaoImpl;
import com.lovestudying.dao.Impl.CourseDaoImpl;
import com.lovestudying.dao.Impl.OrderDaoImpl;
import com.lovestudying.entity.Account;
import com.lovestudying.entity.Course;
import com.lovestudying.entity.Order;
import com.lovestudying.service.AccountService;
import com.lovestudying.service.OrderService;
import com.lovestudying.util.PageInfo;

public class OrderServiceImpl implements OrderService {

	// 调用持久化层OrderDao-OrderDaoImpl
	OrderDao orderDao = new OrderDaoImpl();

	// 调用持久化层AccountDao-AccountDaoImpl
	AccountDao accountDao = new AccountDaoImpl();

	// 调用持久化层CourseDao-CourseDaoImpl
	CourseDao courseDao = new CourseDaoImpl();

	@Override
	public PageInfo<Order> getOrder_Where(Map<String, Object> Where_Param) {
		// 按条件查找Order(Where)
		return orderDao.findOrderUniAccount(Where_Param, null, null, null);

	}

	@Override
	public List<Order> getOrderView(String account_phone) {

		// 根据手机号获取Account对象
		Account account = accountDao.findAccountByPhone(account_phone);

		// 从而获取账户Id
		String accountId = account.getAccount_id();

		// 通过账户Id获取PageInfo<Order>
		List<Order> pageInfo = orderDao.findOrderByState(accountId);

		// 定义一个字符串
		String orderId = "";
		List<Course> list = null;
		List<Order> listAll = new ArrayList<Order>();

		// 遍历结果集
		for (Order order : pageInfo) {
			// 获取订单
			orderId = order.getOrder_id();

			// 根据订单Id查询课程Id
			list = courseDao.findCourseByOrderId(orderId);

			order.setCourse_List(list);

			listAll.add(order);

		}

		return listAll;
	}

	@Override
	public Integer UpdateOrderByOrderId(Map<String, Object> Set_Param, Map<String, Object> Where_Param) {
		// TODO Auto-generated method stub
		return orderDao.UpdateOrder(Set_Param, Where_Param);
	}

	@Override
	public Boolean isBought(String accountId, String courseId) {
		Map<String, Object> Where_Param = new HashMap<>();
		Where_Param.put("`order`.account_id", accountId);
		// TODO 自动生成的方法存根
		Where_Param.put("order_detail.course_id", courseId);
		if (orderDao.orderUniOrderDetail(Where_Param, null, null, null).getData().size() != 0) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public String addOrderSingleCourse(String getUUID, String courseId, String accountId, Integer coursePrice,
			String getNowTime) {
		Map<String, Object> Value_Param = new HashMap<>();
		Value_Param.put("order_id", getUUID);
		Value_Param.put("order_state", 1);
		Value_Param.put("order_cost", coursePrice);
		Value_Param.put("account_id", accountId);
		Value_Param.put("order_create_date", getNowTime);
		Integer insertOrder = orderDao.InsertOrder(Value_Param);
		List<String> courseIdList = new ArrayList<>();
		courseIdList.add(courseId);
		String addOrderListData = orderDao.addOrderListData(getUUID, courseIdList);
		if (insertOrder == 1 && "success".equals(addOrderListData)) {
			accountDao.updateAccountSubtractIntegral(accountId, coursePrice);
			return "success";
		} else if (insertOrder == 1 && !"success".equals(addOrderListData)) {
			return addOrderListData;
		} else {
			return "fail";
		}
	}

}
